package com.example.experiment4sqlite.adapter

import android.app.AlertDialog
import android.content.Intent
import android.service.autofill.UserData
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import android.widget.Toast
import androidx.recyclerview.widget.RecyclerView
import com.example.experiment4sqlite.FindAndDelUserActivity
import com.example.experiment4sqlite.R
import com.example.experiment4sqlite.TAG
import com.example.experiment4sqlite.bean.UserInfo
import com.example.experiment4sqlite.utils.UserDbHelper

class MyAdapter(private val myDataset: List<UserInfo>) :
        RecyclerView.Adapter<MyAdapter.MyViewHolder>(){

//    class MyViewHolder(val textView: TextView) : RecyclerView.ViewHolder(textView)
    class MyViewHolder(v: View, p: ViewGroup) : RecyclerView.ViewHolder(v) {
        val textView: TextView = v.findViewById(R.id.user_name)
        val textViewStuNum: TextView = v.findViewById(R.id.num)
        val textView3: TextView = v.findViewById(R.id.sex)
        val textView4: TextView = v.findViewById(R.id.age)

        init {
            // Define click listener for the ViewHolder's View.
            v.setOnClickListener {
                val stuNum: String = textViewStuNum.text.toString()
                //弹出一个对话框
                AlertDialog.Builder(p.context)
                    .setTitle("系统提示")
                    .setMessage("确定删除该条数据吗！")
                    .setPositiveButton("确定") { dialog, which ->
                        val user = UserDbHelper(p.context)
                        val db = user.writableDatabase
                        user.delUser(db, stuNum)
                        refresh(p)
                        // 不能这样跳转
//                        FindAndDelUserActivity().refresh()
                        Toast.makeText(p.context, "删除成功", Toast.LENGTH_SHORT).show()
                    }.setNegativeButton("取消") { dialog, which -> }
                    .show()
            }
        }

        private fun refresh(p: ViewGroup) {
            // 强转
            val activity: FindAndDelUserActivity = p.context as FindAndDelUserActivity
            activity.finish()
            val intent = Intent(activity, FindAndDelUserActivity::class.java)
            activity.startActivity(intent)
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
        val v = LayoutInflater.from(parent.context)
            .inflate(R.layout.my_item_view, parent, false)

        return MyViewHolder(v, parent)
    }

    override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
        holder.textView.text = myDataset[position].userName
        holder.textViewStuNum.text = myDataset[position].stuNumber
        holder.textView3.text = myDataset[position].sex
        holder.textView4.text = myDataset[position].age.toString()
    }

    override fun getItemCount(): Int{
        Log.d(TAG, myDataset.size.toString())
        return myDataset.size
    }
}